from django.db import models
import os
from user.models import User

# Create your models here.
class Category(models.Model):
    class Meta:
        verbose_name_plural = '课程种类表'

    name = models.CharField(max_length=64, unique=True, verbose_name='课程种类')

    def __str__(self):
        return self.name

def path_to_save_video(instance, filename):
    return os.path.join('static', 'video', filename)

def path_to_save_img(instance, filename):
    return os.path.join('static', 'img', filename)

class Course(models.Model):
    STATUS_CHOICES = (
        (0, '免费'),
        (1, '收费')
    )
    class Meta:
        verbose_name_plural = '课程表'

    courseName = models.CharField(max_length=64, verbose_name='课程名')
    fileName = models.FileField(upload_to=path_to_save_video, verbose_name='课程视频文件')
    imgName = models.FileField(upload_to=path_to_save_img, verbose_name='课程封面')
    pCategory = models.ForeignKey(to=Category, related_name='course_set',on_delete=models.CASCADE, verbose_name='类别')
    price = models.DecimalField(max_digits=7, decimal_places=2, default=0, verbose_name='价格', blank=True)
    summary = models.TextField(max_length=1024, default='', blank=True, verbose_name='简介')
    status = models.PositiveSmallIntegerField(default=0, blank=True, choices=STATUS_CHOICES, verbose_name='状态') #0:免费 1:收费
    createDatetime = models.DateTimeField(auto_now_add=True, verbose_name='创建时间', blank=True)
    userBuyer = models.ManyToManyField(to=User, related_name='courseBuyed_s', verbose_name='购买用户', blank=True)
    userShoppingCart = models.ManyToManyField(to=User, related_name='courseInShopping_s', verbose_name='加入购物车的用户', blank=True)

    def __str__(self):
        return self.courseName
